<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Staff_model extends CI_Model {
	public function __construct() {
		parent::__construct ();
	}
	/*
	 *得到所有员工信息
	 */
	public function find_all_staffs(){
		$this->db->select('t_staff.id,t_staff.name,t_staff.gender,t_staff.idcard,t_staff.birthday,t_staff.degree,t_staff.address,t_staff.bankcard,t_staff.phone,t_staff.type,t_staff.departmentid,t_department.name as dname,t_staff.security,t_staff.fund,t_staff.insurance,t_staff.state,t_staff.start_date,t_staff.end_date');
		$this->db->from('t_staff');
		$this->db->join('t_department', 't_staff.departmentid = t_department.id','left');
		$query=$this->db->get();
		return $query->result_array();
	}
	/*
	 * 返回员工的数量
	 */
	public function count_staffs($name,$department,$type,$security,$fund,$state){
		$this->db->like('name', $name);
		if($department!=0){
			$this->db->where('departmentid',$department);
		}
		if($type!=-1){
			$this->db->where('type',$type);
		}
		if($security!=-1){
			$this->db->where('security',$security);
		}
		if($fund!=-1){
			$this->db->where('fund',$fund);
		}
		if($state!=-1){
			$this->db->where('state',$state);
		}
		$query = $this->db->get('t_staff');
		return $query->num_rows();
	}
	/*
	 * 根据分页信息得到员工信息
	 */
	public function find_staffs($name,$department,$type,$security,$fund,$state,$per_page,$offset){
		$this->db->select('t_staff.id,t_staff.name,t_staff.gender,t_staff.idcard,t_staff.birthday,t_staff.degree,t_staff.address,t_staff.bankcard,t_staff.phone,t_staff.salary,t_staff.type,t_staff.departmentid,t_department.name as dname,t_staff.security,t_staff.fund,t_staff.insurance,t_staff.state,t_staff.start_date,t_staff.end_date');
		$this->db->from('t_staff');
		$this->db->join('t_department', 't_staff.departmentid = t_department.id','left');
		$this->db->like('t_staff.name', $name);
		if($department!=0){
			$this->db->where('departmentid',$department);
		}
		if($type!=-1){
			$this->db->where('type',$type);
		}
		if($security!=-1){
			$this->db->where('security',$security);
		}
		if($fund!=-1){
			$this->db->where('fund',$fund);
		}
		if($state!=-1){
			$this->db->where('state',$state);
		}
		$this->db->limit($per_page, $offset);
		$this->db->order_by("id", "desc");
		$query=$this->db->get();
		return $query->result_array();
	}
	/*
	 * 根据ID得到员工信息
	 */
	public function load($id){
		$query = $this->db->get_where('t_staff',array('id' => $id));
		if ($row = $query->row_array()){
			return $row;
		}
		return array();
	}
	/*
	 * 添加员工信息
	 */
	public function create($staff){
		$this->db->insert('t_staff', $staff);
		return $this->db->insert_id();
	}
	/*
	 * 更新员工信息
	 */
	public function update($id,$staff){
		$this->db->where('id', $id);
		$this->db->update('t_staff', $staff);
		return $this->db->affected_rows()>0;
	}
	/*
	 * 删除员工信息
	 */
	public function delete($id){
		$this->db->where('id', $id);
		$this->db->delete('t_staff');
		return $this->db->affected_rows()>0;
	}
	/*
	 * 根据部门得到员工信息
	 */
	public function find_staffs_by_dep($departmentid){
	    $this->db->where('departmentid',$departmentid);
		$query = $this->db->get('t_staff');
		return $query->result_array();
	}
}